package main

import (
	"fmt"
	"time"
	"net/http"

	"session"
	"github.com/go-redis/redis"
)

func main()  {

	//err := session.Initialize()
	err := session.Initialize(session.Option{Store:session.NewStoreRedis(redis.NewClient(&redis.Options{Addr: "192.168.1.38:6383"}))}, )
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "hello word")
	})

	http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
		se, _ := session.Start(w, r)
		value := time.Now().Unix()
		se.Set("session-value", value)
		fmt.Fprintln(w, "session-id:", se.ID(), "session-value:", value)
	})

	http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
		se, _ := session.Start(w, r)
		fmt.Fprintln(w, "session-id:", se.ID(), "session-value:", se.Get("session-value"))
	})

	http.HandleFunc("/del", func(w http.ResponseWriter, r *http.Request) {
		se, _ := session.Start(w, r)
		se.Del("session-value")
		fmt.Fprintln(w, "delete session-id:", se.ID())
	})

	http.HandleFunc("/clear", func(w http.ResponseWriter, r *http.Request) {
		se, _ := session.Start(w, r)
		se.Clear()
		fmt.Fprintln(w, "session clear")
	})

	http.ListenAndServe(":8283", nil)
}
